package com.abe.mvp.presenter;

import com.abe.mvp.contact.SerialPortContact;
import com.abe.mvp.model.SerialPortModelImp;
import com.abe.mvp.model.bean.SerialPortDataBean;

public class SerialPortPresenterImp implements SerialPortContact.Presenter,
		SerialPortContact.Model.ISerialPortBack {

	private SerialPortContact.View mView;
	private SerialPortContact.Model mModel;

	public SerialPortPresenterImp(SerialPortContact.View mView) {
		this.mView = mView;
		this.mModel = SerialPortModelImp.from(this);
	}

	@Override
	public void onResult(int code, Object data, String errorMSG) {
		switch (code) {
		case SerialPortModelImp.CODE_OPEN_SUCCESS:
			mView.onConnectSuccess();
			break;
		case SerialPortModelImp.CODE_CLOSE_SUCCESS:
			mView.onCloseSuccess();
			break;
		case SerialPortModelImp.CODE_SEND_DATA_SUCCESS:
			mView.onSendDataSuccess();
			break;
		case SerialPortModelImp.CODE_RECEIVE_DATA_SUCCESS:
			SerialPortDataBean bean = (SerialPortDataBean) data;
			/** 数据校验 */
			mView.onReceiveDataSuccess(bean);
			break;
		case SerialPortModelImp.CODE_ERROR:
			mView.onError(errorMSG);
			break;
		default:
			break;
		}
	}

	@Override
	public void openSerialPort(int type) {
		mModel.openSerialPort(type);
	}

	@Override
	public void sendCommand(byte[] buffer) {
		mModel.sendCommand(buffer);
	}

	@Override
	public void closeSerialPort() {
		mModel.closeSerialPort();
	}

}
